package com.td.module.equipment.controller.admin.acquisitioninfo;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.equipment.controller.admin.acquisitioninfo.vo.*;
import com.td.module.equipment.convert.acquisitioninfo.AcquisitionInfoConvert;
import com.td.module.equipment.dal.dataobject.acquisitioninfo.AcquisitionInfoDO;
import com.td.module.equipment.service.acquisitioninfo.AcquisitionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 设备采集信息")
@RestController
@RequestMapping("/equipment/acquisition-info")
@Validated
public class AcquisitionInfoController {

    @Resource
    private AcquisitionInfoService acquisitionInfoService;

    @PostMapping("/create")
    @ApiOperation("创建设备采集信息")
    public CommonResult<Long> createAcquisitionInfo(@Valid @RequestBody AcquisitionInfoCreateReqVO createReqVO) {
        return success(acquisitionInfoService.createAcquisitionInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备采集信息")
    public CommonResult<Boolean> updateAcquisitionInfo(@Valid @RequestBody AcquisitionInfoUpdateReqVO updateReqVO) {
        acquisitionInfoService.updateAcquisitionInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备采集信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteAcquisitionInfo(@RequestParam("id") Long id) {
        acquisitionInfoService.deleteAcquisitionInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备采集信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<AcquisitionInfoRespVO> getAcquisitionInfo(@RequestParam("id") Long id) {
        AcquisitionInfoDO acquisitionInfo = acquisitionInfoService.getAcquisitionInfo(id);
        return success(AcquisitionInfoConvert.INSTANCE.convert(acquisitionInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得设备采集信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<AcquisitionInfoRespVO>> getAcquisitionInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<AcquisitionInfoDO> list = acquisitionInfoService.getAcquisitionInfoList(ids);
        return success(AcquisitionInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/get-group-monitor-time")
    @ApiOperation("获得设备采集信息列表根据stationId并且根据采集时间分组")
    public CommonResult<Map<LocalDateTime, List<AcquisitionInfoRespVO>>> getInfoGroupMonitorTime(@Valid AcquisitionInfoPageReqVO pageVO) {
        List<AcquisitionInfoRespVO> list = AcquisitionInfoConvert.INSTANCE.
                convertList(acquisitionInfoService.getInfoGroupMonitorTime(pageVO));
        Map<LocalDateTime, List<AcquisitionInfoRespVO>> map = list.stream().
                collect(Collectors.groupingBy(AcquisitionInfoRespVO::getMonitorTime));
        return success(map);
    }

    @GetMapping("/sum")
    @ApiOperation("统计水质数据")
    public CommonResult<Integer> sumWaterQuality() {
        return success(acquisitionInfoService.sumWaterQuality());
    }

    @GetMapping("/page")
    @ApiOperation("获得设备采集信息分页")
    public CommonResult<PageResult<AcquisitionInfoRespVO>> getAcquisitionInfoPage(@Valid AcquisitionInfoPageReqVO pageVO) {
        PageResult<AcquisitionInfoDO> pageResult = acquisitionInfoService.getAcquisitionInfoPage(pageVO);
        return success(AcquisitionInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出设备采集信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportAcquisitionInfoExcel(@Valid AcquisitionInfoExportReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<AcquisitionInfoDO> list = acquisitionInfoService.getAcquisitionInfoList(exportReqVO);
        // 导出 Excel
        List<AcquisitionInfoExcelVO> datas = AcquisitionInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备采集信息.xls", "数据", AcquisitionInfoExcelVO.class, datas);
    }

}
